﻿using System.Collections.Generic;
using UnityEngine;
namespace UGUIMan
{
    public class Map:MonoBehaviour
    {
        public static List<GameObject> mappart=new List<GameObject>();
        public static GameObject root;
        public float mappartLength;
        public static float heightdelta;
        public static float index = 0;
        public float upForce;
        public static Mappiece endmap
        {
            set
            {
                killold();
                oldmap = value;
            }
            get
            {
                return oldmap;
            }
        }
        public static Mappiece oldmap;
        public static void killold()
        {
            if (oldmap != default(Mappiece))
            {
                oldmap.transform.GetChild(0).transform.SetParent(oldmap.transform.parent);
                if (mappart.Exists(g => g.Equals(
                     oldmap.gameObject)))
                {
                    mappart.Remove(oldmap.gameObject);
                }
                GameObject.Destroy(oldmap.gameObject);
            }
        }
        public static Mappiece startmap
        {
            set
            {
                startedmap = value;
                AddMap();
            }
            get
            {
                return startedmap;
            }
        }
        public static Mappiece startedmap;
        public static bool lastup;
        public static float speed;
        private void Awake()
        {
            root = GameObject.Find("FloorRoot");
        }
        public static Mappiece AddMap()
        {
            GameObject np=new GameObject();
            if (startedmap.transform.rotation.eulerAngles.z<50||startedmap.transform.rotation.eulerAngles.z>310)
            {
                int i = Random.Range(0, 3);
                if (i>1)
                {
                    np = Loadup();
                }
                else if(i>0)
                {
                    np = Loaddown();
                }
                else
                {
                    np = LoadIndex();
                }
            }
            else if(startedmap.transform.rotation.eulerAngles.z < 100 || startedmap.transform.rotation.eulerAngles.z > 260)
            {
                if (lastup)
                {
                    np = Loaddown();
                }
                else
                {
                    np = Loadup();
                }
            }
            else
            {
                np = LoadIndex();
            }
            np.tag = "map";
            np.name += index;
            index++;
            np.AddComponent<Mappiece>();
            mappart.Add(np);
            Debug.Log(np.name + ":" + np.transform.rotation.eulerAngles);
            heightdelta += np.transform.position.y-20;
            return np.GetComponent<Mappiece>();
        }
        public static GameObject Loaddown()
        {


            var g = GameObject.Instantiate((GameObject)Resources.Load("Prefab/FloorDown"), new Vector3(150, 25, 0), Quaternion.Euler(0, 0, Random.Range(-20, 0))) as GameObject;
            lastup = !lastup;
            g.transform.SetParent(startedmap.transform, false);
            g.transform.rotation=Quaternion.identity;
            //g.transform.localScale = new Vector3(1, 1, 1);
            //g.GetComponent<RectTransform>().anchoredPosition = new Vector2(150, -25);
            return g;
        }
        public static GameObject Loadup()
        {
            

            var g = GameObject.Instantiate((GameObject)Resources.Load("Prefab/FloorUp"),new Vector3(150,-25,0),Quaternion.Euler(0,0,Random.Range(0,40)))as GameObject;
            lastup = true;
            g.transform.SetParent(startedmap.transform,false);
            //g.transform.localScale = new Vector3(1, 1, 1);
            //g.GetComponent<RectTransform>().anchoredPosition = new Vector2(150, -25);
            return g;
        }
        public static GameObject LoadIndex()
        {
            var g = GameObject.Instantiate((GameObject)Resources.Load("Prefab/FloorDown"), new Vector3(150, 25, 0), Quaternion.Euler(0, 0, Random.Range(-20, 0))) as GameObject;
            lastup = false;
            g.transform.SetParent(startedmap.transform,false);
            //g.transform.localScale = new Vector3(1, 1, 1);
            //g.GetComponent<RectTransform>().anchoredPosition = new Vector2(150, 25);
            return g;
        }
        public static bool MapHit(Vector2 lt,Vector2 lb,Vector2 rt,Vector2 rb)
        {
            foreach(GameObject m in mappart)
            {
                Mappiece mp = m.GetComponent<Mappiece>();
                mp.Hit(lt);
            }
            return false;
        }

        public bool MapCheck()
        {
            if (mappart.Count < Screen.width / mappartLength + 2)
            {
                if (Screen.width - mappart[mappart.Count - 1].transform.position.x < mappartLength / 2)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }

        public void AddPiece()
        {
            int index = Random.Range(0, 10);
            GameObject Piece = GameObject.Instantiate((GameObject)Resources.Load(""));
        }
        private void Update()
        {
            transform.position -= new Vector3(1, 0, 0)*Feets.speed/5;
            float heightavg=0;
            mappart.ForEach(g => heightavg += g.transform.position.y+60);
            heightavg /= mappart.Count;
            float heightTemp = Mathf.Lerp(transform.position.y, transform.position.y + heightavg, Time.deltaTime);
            transform.position += new Vector3(0,  transform.position.y-heightTemp, 0);
        }
    }
}